import { customTable } from '@/components/ProTable/interface';
import { ProTableValue } from '@/interface/public';

export function useDataGrid(field: 'left' | 'right', tableRef: ProTableValue, refresh: Function) {
  let dataGrid = reactive<customTable>({
    initParam: {
      roleId: null,
      neRoleId: null
    },
    // 表格配置项
    columns: [
      {
        type: 'selection',
        fixed: 'left'
      },
      { prop: 'operation', label: 'operation', fixed: 'left', width: 120 },

      {
        isShow: false,
        prop: 'key',
        search: {
          el: 'input',
          key: 'key',
          label: '关键值'
        }
      },
      {
        prop: 'name',
        label: '角色'
      }
    ],
    headButtonList: [
      {
        label: '批量删除',
        type: 'danger',
        icon: 'svg-scrap',
        // permissionCode: 'p:auth:role:remove',
        display: () => field === 'right',
        methods: async row => {
          const ids = tableRef.value.selectedListIds;
          if (!ids?.length) {
            elMessage('请选择删除的数据');
            return;
          }
          const params = {
            roleId: dataGrid.initParam.roleId,
            userIds: ids.join(',')
          };
          await userRoleDelete(params);
          elMessage();
          refresh();
        }
      },
      {
        label: '批量添加',
        type: 'primary',
        icon: 'Plus',
        // permissionCode: 'p:auth:role:remove',
        display: () => field === 'left',
        methods: async row => {
          const ids = tableRef.value.selectedListIds;

          if (!ids?.length) {
            elMessage('请选择删除的数据');
            return;
          }
          console.log(ids, ids.join(','), 'ids');

          const params = {
            roleId: dataGrid.initParam.roleId,
            userIds: ids.join(',')
          };
          await userRoleAddByAppend(params);
          elMessage();
          refresh();
        }
      }
    ],
    rowButtonList: [
      {
        label: 'delete',
        type: 'danger',
        // permissionCode: 'p:auth:role:remove',
        display: () => field === 'right',
        methods: async row => {
          const params = {
            roleId: dataGrid.initParam.roleId,
            userIds: row.id
          };
          await userRoleDelete(params);
          elMessage();
          refresh();
        },
        attrs: {
          link: true
        }
      },
      {
        label: '添加',
        type: 'primary',
        // permissionCode: 'p:auth:role:remove',
        display: () => field === 'left',
        methods: async row => {
          const params = {
            roleId: dataGrid.initParam.roleId,
            userIds: row.id
          };
          await userRoleAddByAppend(params);
          elMessage();
          refresh();
        },
        attrs: {
          link: true
        }
      }
    ]
  });

  // 对表格返回数据信息处理
  const tableDataCallback = data => {
    data.records = data.records.filter(item => item.nick !== '超级管理员');
    data.records.forEach(item => {
      item.name = item.nick ? `${item.nick}(${item.username})` : item.nick;
    });
    return data;
  };
  return {
    dataGrid,
    tableDataCallback
  };
}
